package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rainng.coursesystem.dao.BaseDAO;
import com.rainng.coursesystem.dao.mapper.TrainingGoalMapper;
import com.rainng.coursesystem.model.entity.TrainingGoalEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TrainingGoalDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;

    private final TrainingGoalMapper mapper;

    public TrainingGoalDAO(TrainingGoalMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(TrainingGoalEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public TrainingGoalEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(TrainingGoalEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String name) {
        LambdaQueryWrapper<TrainingGoalEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, TrainingGoalEntity::getName, name);

        return mapper.selectCount(wrapper);
    }

    public List<TrainingGoalEntity> getPage(Integer index, String name) {
        LambdaQueryWrapper<TrainingGoalEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, TrainingGoalEntity::getName, name);

        return selectPage(mapper, wrapper, index, PAGE_SIZE);
    }

    public List<TrainingGoalEntity> listName() {
        LambdaQueryWrapper<TrainingGoalEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TrainingGoalEntity::getId, TrainingGoalEntity::getName);

        return mapper.selectList(wrapper);
    }
}
